package com.blue.controller.system;


import cn.hutool.core.collection.CollectionUtil;
import com.blue.common.annotation.NotNull;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.annotation.ReplaceParam;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.ConvertUtils;
import com.blue.common.utils.QueryWrapperUtils;
import com.blue.dto.system.DictDTO;
import com.blue.po.system.DictItemPO;
import com.blue.po.system.DictPO;
import com.blue.service.system.IDictService;
import com.github.yulichang.query.MPJQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 数据字典表 前端控制器
 * </p>
 *
 * @author 陈南杰
 * @since 2023-09-22
 */
@RestController
@RequestMapping("/system/dict")
@Tag(name = "系统字典")
public class DictController {
    @Autowired
    private IDictService dictService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    @ReplaceParam("dictName,dictCode")
    public ResponseResult<?> pageList(DictDTO requestDTO,
                                      @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        DictPO dictPO = ConvertUtils.convert(DictPO.class, requestDTO);
        MPJQueryWrapper<DictPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(dictPO);
        Page<DictPO> page = new Page<>(pageNum, pageSize);
        Page<DictPO> pageResult = dictService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增字典")
    public ResponseResult<?> add(@RequestBody DictDTO requestDTO) {
        List<DictPO> dictPOList = dictService.list(new LambdaQueryWrapper<DictPO>()
                .eq(DictPO::getDictCode, requestDTO.getDictCode()));
        if (CollectionUtil.isNotEmpty(dictPOList)) {
            return ResponseResult.errorResult("该字典编号系统已存在，请重新输入");
        }
        DictPO dictPO = ConvertUtils.convert(DictPO.class, requestDTO);
        dictService.save(dictPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除字典")
    @NotNull(value = "ids", message = "字典ID不能为空！")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        dictService.delete(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改字典")
    public ResponseResult<?> edit(@RequestBody DictDTO requestDTO) {
        DictPO dictPO = ConvertUtils.convert(DictPO.class, requestDTO);
        dictService.updateById(dictPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody DictDTO requestDTO) {
        DictPO dictPO = dictService.getById(requestDTO.getId());
        return ResponseResult.okResult(dictPO);
    }

    @GetMapping("/getDictItems")
    @Operation(summary = "通过字典编码获取所有子字典")
    public ResponseResult<?> getDictItems(@RequestParam("dictCode") String dictCode) {
        List<DictItemPO> dictItemPOS = dictService.getByDictCode(dictCode);
        return ResponseResult.okResult(dictItemPOS);
    }

    @GetMapping("/getDictItem")
    @Operation(summary = "通过字典编码和子字典编码获取子字典信息")
    public ResponseResult<?> getDictItem(@RequestParam("dictCode") String dictCode, @RequestParam("itemCode") String itemCode) {
        DictItemPO dictItemPO = dictService.getByDictCodeAndItemCode(dictCode, itemCode);
        return ResponseResult.okResult(dictItemPO);
    }

}
